package org.flint92.springcloud.feign.provider.service;

import org.flint92.user.domain.User;
import org.flint92.user.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author flint92
 */
@Service
public class InMemoryUserService implements UserService {

    private final Map<Long, User> cache = new ConcurrentHashMap<>();

    @Override
    public boolean saveUser(User user) {
        Assert.notNull(user, "User must not be null.");

        cache.put(user.getId(), user);
        return true;
    }

    @Override
    public List<User> findAll() {
        return new ArrayList<>(cache.values());
    }
}
